$(function () {


    $('.select_card .select_card_01 li').click(function () {
        if ($(this).find('.Select').hasClass('Select_02')) {
            $(this).find('.Select').removeClass('Select_02');
        } else {
            $(this).find('.Select').addClass('Select_02');
            $(this).siblings().find('.Select').removeClass('Select_02');
        }
    });


    $('.color .box_01 ul li').click(function () {
        if ($(this).find('.icon_01').hasClass('icon_02')) {
            $(this).find('.icon_01').removeClass('icon_02');
        } else {
            $(this).find('.icon_01').addClass('icon_02');
            $(this).siblings().find('.icon_01').removeClass('icon_02');
        }
    });

    $('.content .protocol .agreement').click(function () {
        $('.background_01').show();
        $('.service_agreement').show();
    });
    $('.background_01').click(function () {
        $(this).hide();
        $('.service_agreement').hide();
    });
    $('.service_agreement .close').click(function(){
        $('.service_agreement').hide();
        $('.background_01').hide();
    });






    //获取验证码
    var counting = false;
    $('.content .info_bar .code_btn').click(function () {

        var timer, count = 60,
            that = $(this);

        var phone_01 = $.trim($('.content .info_bar .phone').val());
        var $btn = $(this);
        if (!checkPhone(phone_01)){
            alert("手机号码有误，请重填");
            return false;
        }
        /*if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone_01))) {
            alert("手机号码有误，请重填");
            return false;
        }*/
        $(this).attr("disabled", true);

        $.ajax({
            url: "/sms/snedSMS.html",
            data: {'phone': phone_01},
            type: "POST",
            dataType: "json",
            success: function (res) {
                /*
                 * @weisf
                 * 手机格式不正确
                 */
                if (res.code != 1) {
                    alert(res.msg);
                    $btn.text('获取验证码');
                    return;
                }
                /* 倒计时 */
                /*延迟一秒问题*/
                setTimeout(function () {
                    that.html(count + '(s)');
                }, 0)
                /*一分钟后再次发送*/
                timer = setInterval(function () {
                    if (count > 1) {
                        count--;
                        // alert($(this).html())
                        that.html(count + '(s)');
                    } else {
                        counting = false;
                        count = 60;
                        that.html("获取验证码");

                        clearInterval(timer);
                        $('.content .info_bar .code_btn').attr("disabled", false);
                    }

                }, 1000);
                console.log("222222222222222====================>>>>>>>>>>>>>>");
            },
            error: function () {
                console.log("111111111====================>>>>>>>>>>>>>>");
                $btn.text('获取失败');
            }
        });


    });


    $('.bottom_btn .submit_go').click(function () {

        var name = $.trim($('.content .info_bar .name').val()); //名字
        var phone_01 = $.trim($('.content .info_bar .phone').val()); //电话
        var code = $.trim($('.content .info_bar .code_Verification').val()); //验证码
        var picker = $.trim($('#picker').val());  //省、市、区
        var address = $.trim($('.content .info_bar .address').val()); //地址

        var flow_card = $('.Select_02').parent('li').attr("title");
        var color = $('.icon_02').parent('li').attr("title");
        if (flow_card == undefined) {
            alert('请选择话费套餐!');
            return;
        }
        if (color == undefined) {
            alert('请选手表颜色!');
            return;
        }
        var productEntityIds = flow_card + "," + color+"," + 1514267657851017;
        var activityId=$("#activityId").val();

        console.log(productEntityIds);
        var orderSourceId = 1;
        var distributorCode = $('#distributorCode').val();


        var postdata = {
            'name': name,
            'phone': phone_01,
            'code': code,
            'address': picker + address,
            'activityIds': activityId,
            'productEntityIds': productEntityIds,
            'orderSourceId': orderSourceId,
            'distributorCode': distributorCode
        };
        console.log(postdata);
        if (name == '') {
            alert("请输入姓名!");
            return false;
        }
        if (phone_01 == '') {
            alert("请输入电话号码!");
            return false;
        }
        if (!checkPhone(phone_01)){
            alert("手机号码有误，请重填");
            return false;
        }
        /*if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone_01))) {
            alert("手机号码有误，请重填");
            return false;
        }*/
        if (code == '') {
            alert("请输入验证码!");
            return false;
        }
        if(activityId==null || activityId=='' || activityId==undefined){
        if (picker == ''){
            alert("请选择市区!");
            return false;
        }
        if (address == '') {
            alert("请输入收货地址!");
            return false;
        }
        }
        var $checkbox1 = $('#checkbox1');
        console.log($checkbox1.prop('checked'));
        if ($checkbox1.prop('checked') != true) {
            alert('请勾选秀才4G•AI智能手表用户服务协议才能提交！');
            return;
        }

        $.ajax({
            url: "/apply/applyWatch.html",
            data: postdata,
            type: "POST",
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.code != 1) {
                    alert(data.msg);
                    return;
                }
                location.href = "/pay/pay/ " + data.data.orderId + " .html";
            },
            error: function (data) {

                console.log(data);

                alert("错误！！！");
            }
        })


    })


});

function disable(){
    document.getElementById("accept").disabled=true
}
function enable(){
    document.getElementById("accept").disabled=false
}